const connection = require("../app/database");

class FileService {
	// 保存头像
	async saveAvatarInfo(filename, mimetype, size, user_id) {
		// 将头像存储到数据库
		const statement = `INSERT INTO avatar (filename, mimetype, size, user_id) VALUES(?, ?, ?, ?);`;
		const [result] = await connection.execute(statement, [
			filename,
			mimetype,
			size,
			user_id
		]);

		// 返回数据
		return result;
	}

	// 查询头像
	async getAvatarInfo(userId) {
		const statement = `SELECT * FROM avatar WHERE user_id = ?;`;
		const [result] = await connection.execute(statement, [userId]);
		// 返回结果
		return result.pop();
	}

	// 保存内容图片
	async savePictureInfo(filename, mimetype, size, moment_id, user_id) {
		// 将图片信息存储到数据库
		const statement = `INSERT INTO file (filename, mimetype, size, moment_id, user_id) VALUES(?, ?, ?, ?, ?);`;
		const [result] = await connection.execute(statement, [
			filename,
			mimetype,
			size,
			moment_id,
			user_id
		]);

		// 返回数据
		return result;
	}

	// 查询内容图片
	async getPictureInfo(filename) {
		const statement = `SELECT * FROM file WHERE filename = ?;`;
		const [result] = await connection.execute(statement, [filename]);
		// 返回结果
		return result[0];
	}
}

module.exports = new FileService();
